package com.ximi.ximimall.goods.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ximi.ximimall.common.component.LocalCache;
import com.ximi.ximimall.common.util.ResponseUtil;
import com.ximi.ximimall.goods.service.LitemallAdService;
import com.ximi.ximimall.goods.service.LitemallBrandService;
import com.ximi.ximimall.goods.service.LitemallTopicService;
import com.ximi.ximimall.model.LitemallAd;
import com.ximi.ximimall.model.LitemallBrand;
import com.ximi.ximimall.model.LitemallGoods;
import com.ximi.ximimall.model.LitemallTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 西米的风
 * @description TODO
 * @date 2022-10-23 17:43
 */
@RestController
@RequestMapping("/wx/ad")
public class AdController {
    @Autowired
    private LitemallAdService adService;

    @Autowired
    private LitemallBrandService brandService;

    @Autowired
    private LitemallTopicService topicService;

    @Autowired
    private LocalCache localCache;

    private static String AD_INDEX_KEY = "ad_index_key";

    private static String AD_BRAND_KEY = "ad_brand_key";

    private static String AD_TOPIC_KEY = "ad_topic_key";

    @RequestMapping("/banner/queryIndex")
    public Object queryIndex() {
        List<LitemallAd> list  = (List<LitemallAd>) localCache.get(AD_INDEX_KEY);
        if(CollectionUtil.isNotEmpty(list)){
            return ResponseUtil.ok(list);
        }
        list  = adService.queryIndex();
        localCache.setLocalCache(AD_INDEX_KEY,list);
        return ResponseUtil.ok(list);
    }

    @RequestMapping("/topic/query")
    public Object queryTopic() {
        List<LitemallTopic> list  = (List<LitemallTopic>) localCache.get(AD_TOPIC_KEY);
        if(CollectionUtil.isNotEmpty(list)){
            return ResponseUtil.ok(list);
        }
        list  = topicService.queryList(0, 4);
        localCache.setLocalCache(AD_TOPIC_KEY,list);
        return ResponseUtil.ok(list);
    }

    @RequestMapping("/brand/query")
    public Object queryBrand() {
        List<LitemallBrand> list  = (List<LitemallBrand>) localCache.get(AD_BRAND_KEY);
        if(CollectionUtil.isNotEmpty(list)){
            return ResponseUtil.ok(list);
        }
        list  = brandService.query(0, 4);
        localCache.setLocalCache(AD_BRAND_KEY,list);
        return ResponseUtil.ok(list);
    }
}
